<?php
namespace app\index\controller;

use app\common\controller\UserController;
use app\common\model\EmployPurchase;

class Employ extends UserController
{
    function purchase(){
        $list = EmployPurchase::simpleList([
            "query"=>function($query){
                if(input("employ_id")){
                    $query->where("employ_id",input("employ_id"));
                }
            },
            "result"=>function(&$row){
                $employ = \app\common\model\Employ::getById($row["employ_id"]);
                if(empty($employ)){
                    $row = null;
                }else{
                    $row["employ_avatar"] = $employ->getData("avatar");
                    $row["employ_name"] = $employ->getData("name");
                    $row["image"] = explode(",",$row["image"]);
                }
            }
        ]);
        $this->result_success($list);
    }
    function employ(){
        $list = \app\common\model\Employ::simpleList([ ]);
        $this->result_success($list);
    }

    function one(){
        $data = \app\common\model\Employ::getById(input("id"));
        $this->result_success($data);
    }

    /**
     * 需求
     */
    function purchase_one(){
        $data = \app\common\model\EmployPurchase::getById(input("id"));
        if(!empty($data)){
            $employ = \app\common\model\Employ::getById($data["employ_id"]);
            if($employ){
                $data["employ_avatar"] = $employ["avatar"];
                $data["employ_name"] = $employ["name"];
                $employ = $employ->toArray();
            }
            $data = array_merge($data->toArray(),$employ);
            $data["image"] = explode(",",$data["image"]);
            $this->result_success($data);
        }
        $this->result_error();
    }
    function edit(){
        $param = input();
        $employ = self::$user->getEmploy();
        if(isset( $param["fee"])){
            if($param["fee"]>1000000){
                $this->result_error("交易额不能超过一百万");
            }
        }
        if(empty($employ)){
            $this->result_error("请完善雇主信息",520);
        }else{
            $param["employ_id"] = $employ->id;
            $param["uid"] = $employ->uid;
        }
        if(isset($param["id"])){
            $param["created_time"] = date("Y-m-d H:i:s");
            $param["updated_time"] = date("Y-m-d H:i:s");
            \app\common\model\EmployPurchase::simpleEdit($param);
        }else{
            \app\common\model\EmployPurchase::simpleAdd($param);
        }
        $this->result_success();
    }
    function del(){
        $id = input("id");
        \app\common\model\EmployPurchase::simpleDelete(["query"=>function ($query)use($id){
            $query->where("id",$id);
        }]);
        $this->result_success();
    }
}
